// packageMyInfo/pages/changePassword/changePassword.js
const app = getApp()
let pwdReg = /^[0-9a-z_.@]{6,16}$/
import { throttle } from "../../../utils/util"
const md5 = require('../../../utils/md5')
import { userUpdate } from "../../../public/api/person.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    translations: {},
    language: '',
    oldPassword: '',
    password: '',
    quePassword: '',
    submitMsg: {
      oldPwd: '',
      newPwd: '',
      quePwd: ''
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  doConfirm: throttle(function() {
    let that = this
    if (this.data.oldPassword === '') {
      this.setData({
        ["submitMsg.oldPwd"]: '原密码不可为空'
      })
    }
    if (this.data.password === '') {
      this.setData({
        ["submitMsg.newPwd"]: '新密码不可为空'
      })
    }
    if (this.data.password === '') {
      this.setData({
        ["submitMsg.quePwd"]: '确认密码不可为空'
      })
    }
    if (this.data.submitMsg.oldPwd !== '' ||
      this.data.submitMsg.newPwd !== ''||
      this.data.submitMsg.quePwd !== ''
      ) {
      return false
    }
    let data = {}
    data.userId = wx.getStorageSync('userInfo').id
    data.oldPassword = md5.hexMD5(this.data.oldPassword)
    data.password = md5.hexMD5(this.data.password)
    userUpdate(data).then(res => {
      if (res.data.code === 1) {
        wx.showToast({
          title: '修改成功',
          duration:1500,
          success:function() {
            setTimeout(() => {
              wx.navigateBack({
                delta: 1
              })
            }, 1500)
          }
        })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  }),

  onBlur(e) {
    switch (e.currentTarget.dataset.inputid) {
      case 'oldPwd':
        if (this.data.oldPassword === '') {
          this.setData({
            ["submitMsg.oldPwd"]: '原密码不可为空'
          })
        } else if (!pwdReg.test(this.data.oldPassword)) {
          this.setData({
            ["submitMsg.oldPwd"]: '旧密码格式不正确'
          })
        } else { 
          this.setData({
            ["submitMsg.oldPwd"]: ''
          })
        }
        break;
      case 'newPwd':
        if (this.data.password === '') {
          this.setData({
            ["submitMsg.newPwd"]: '新密码不可为空'
          })
        } else if (!pwdReg.test(this.data.password)) {
          this.setData({
            ["submitMsg.newPwd"]: '新密码格式不正确'
          })
        } else if(this.data.quePassword && this.data.password != this.data.quePassword) {
          this.setData({
            ["submitMsg.newPwd"]: '新密码与确认密码不一致'
          })
        } else {
          this.setData({
            ["submitMsg.newPwd"]: '',
            ["submitMsg.quePwd"]: ''
          })
        }
        break;
      case 'quePwd':
        if (this.data.quePassword === '') {
          this.setData({
            ["submitMsg.quePwd"]: '确认密码不可为空'
          })
        } else if (!pwdReg.test(this.data.quePassword)) {
          this.setData({
            ["submitMsg.quePwd"]: '确认密码格式不正确'
          })
        } else if(this.data.password && this.data.password != this.data.quePassword) {
          this.setData({
            ["submitMsg.quePwd"]: '新密码与确认密码不一致'
          })
        } else {
          this.setData({
            ["submitMsg.quePwd"]: '',
            ["submitMsg.newPwd"]: ''
          })
        }
        break;
      default:
        break;
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let language = wx.getStorageSync('language')
    const translations = app.globalData.translations;
    this.setData({
      language,
      translations
    })
    wx.setNavigationBarTitle({
      title: translations.nav.change_password
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})